package com.wdd.studentmanager.service.Impl;

import com.wdd.studentmanager.domain.SelectedCourse;
import com.wdd.studentmanager.mapper.CourseMapper;
import com.wdd.studentmanager.mapper.SelectedCourseMapper;
import com.wdd.studentmanager.service.SelectedCourseService;
import com.wdd.studentmanager.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class SelectedCourseServiceImpl implements SelectedCourseService {

    @Autowired
    private SelectedCourseMapper selectedCourseMapper;
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 分页查询已选课程
     *
     * @param paramMap 包含分页参数和查询条件
     * @return PageBean<SelectedCourse> 分页结果
     */
    @Override
    public PageBean<SelectedCourse> queryPage(Map<String, Object> paramMap) {
        if (paramMap == null || !paramMap.containsKey("pageno") || !paramMap.containsKey("pagesize")) {
            throw new IllegalArgumentException("Invalid parameters for pagination query");
        }

        // 创建分页对象
        PageBean<SelectedCourse> pageBean = new PageBean<>((Integer) paramMap.get("pageno"), (Integer) paramMap.get("pagesize"));

        // 设置起始索引并查询数据
        paramMap.put("startIndex", pageBean.getStartIndex());
        List<SelectedCourse> selectedCourses = selectedCourseMapper.queryList(paramMap);
        pageBean.setDatas(selectedCourses);

        // 查询总记录数
        Integer totalRecords = selectedCourseMapper.queryCount(paramMap);
        pageBean.setTotalsize(totalRecords);

        return pageBean;
    }

    /**
     * 添加已选课程
     *
     * @param selectedCourse 选课信息
     * @return int 操作结果
     */
    @Override
    @Transactional
    public int addSelectedCourse(SelectedCourse selectedCourse) {
        if (selectedCourse == null) {
            throw new IllegalArgumentException("SelectedCourse cannot be null");
        }

        SelectedCourse existingCourse = selectedCourseMapper.findBySelectedCourse(selectedCourse);
        if (existingCourse == null) {
            int count = courseMapper.addStudentNum(selectedCourse.getCourseId());
            if (count == 1) {
                selectedCourseMapper.addSelectedCourse(selectedCourse);
                return count;
            }
            return count; // 0 for failure to add student number
        }
        return 2; // Course already selected by the student
    }

    /**
     * 删除已选课程
     *
     * @param id 选课记录ID
     * @return int 操作结果
     */
    @Override
    @Transactional
    public int deleteSelectedCourse(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("Course ID cannot be null");
        }

        SelectedCourse selectedCourse = selectedCourseMapper.findById(id);
        if (selectedCourse == null) {
            throw new IllegalArgumentException("SelectedCourse not found for ID: " + id);
        }

        // 减少课程的选课人数
        courseMapper.deleteStudentNum(selectedCourse.getCourseId());
        return selectedCourseMapper.deleteSelectedCourse(id);
    }

    /**
     * 判断学生是否已选课
     *
     * @param studentId 学生ID
     * @return boolean 是否已选课
     */
    @Override
    public boolean isStudentId(int studentId) {
        List<SelectedCourse> selectedCourses = selectedCourseMapper.isStudentId(studentId);
        return selectedCourses.isEmpty();
    }

    /**
     * 根据学生ID获取所有已选课程
     *
     * @param studentId 学生ID
     * @return List<SelectedCourse> 已选课程列表
     */
    @Override
    public List<SelectedCourse> getAllBySid(int studentId) {
        return selectedCourseMapper.getAllBySid(studentId);
    }
}
